package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;
    //构造方法
    public Client() {
        try{
            System.out.println("客户端启动...");
//            socket = new Socket("localhost",8088);
            socket = new Socket("172.234.3.234",8088);
            System.out.println("客户端连接成功...");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //客户端工作的方法
    public  void start(){
        try {
            OutputStream out = socket.getOutputStream();
//            out.write(3);

            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请输入您要发送的信息：");
                String str = sc.next();
                if ("exit".equals(str)){
                    break;
                }
                byte[] data = str.getBytes(StandardCharsets.UTF_8);
                out.write(data.length);
                out.write( data);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (socket != null){
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
